<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User
{
    /**
     * @Common(cnName="#",editType="text",listType="text",width="90")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"user","web"})
     */
    private $id;

    /**
     * @Common(cnName="手机号",editType="text",listType="text",width="")
     * @ORM\Column(type="string", length=255)
     * @Groups({"user","web"})
     */
    private $phone;

    /**
     * @Common(cnName="姓名",editType="text",listType="text",width="")
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"user","web"})
     */
    private $name;

    /**
     * @Common(cnName="头像",editType="pic",listType="text")
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"user","web"})
     */
    private $pic;

    /**
     * @Common(cnName="密码",editType="text",listType="text")
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"user"})
     */
    private $password;

    /**
     * @Common(cnName="注册时间",editType="datetime",listType="text",width="")
     * @ORM\Column(type="datetime", nullable=true)
     * @Groups({"user"})
     */
    private $createAt;

    /**
     * @Common(cnName="会员期限",editType="datetime",listType="text",width="")
     * @ORM\Column(type="datetime", nullable=true)
     * @Groups({"user"})
     */
    private $expireAt;

    /**
     * @Common(cnName="是否可用",editType="switch",listType="text")
     * @ORM\Column(type="boolean")
     * @Groups({"user"})
     */
    private $isOn;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $apiToken;

    /**
     * @Common(cnName="推广手机号",editType="text",listType="text")
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $invitePhone;

    /**
     * @Common(cnName="Openid",editType="switch",listType="text")
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $openid;

    /**
     * @Common(cnName="推荐人",editType="entity",listType="text")
     * @ORM\ManyToOne(targetEntity="App\Entity\User",inversedBy="sons")
     * @ORM\JoinColumn(nullable=true)
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\User",mappedBy="parent")
     * @ORM\OrderBy({"id"="DESC"})
     */
    private $sons;

    /**
     * @Common(cnName="会员等级",editType="entity",listType="text")
     * @ORM\ManyToOne(targetEntity=UserLevel::class, inversedBy="users")
     * @Groups({"user"})
     */
    private $level;

    /**
     * @Common(cnName="会员标记",editType="entity",listType="text")
     * @ORM\ManyToOne(targetEntity=UserGroup::class, inversedBy="users")
     * @Groups({"user"})
     */
    private $groupp;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $passwordPay;

    public function __construct()
    {
        $this->sons = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(?string $password): self
    {
        $this->password = $password;

        return $this;
    }

    public function getCreateAt(): ?\DateTimeInterface
    {
        return $this->createAt;
    }

    public function setCreateAt(?\DateTimeInterface $createAt): self
    {
        $this->createAt = $createAt;

        return $this;
    }

    public function getExpireAt(): ?\DateTimeInterface
    {
        return $this->expireAt;
    }

    public function setExpireAt(?\DateTimeInterface $expireAt): self
    {
        $this->expireAt = $expireAt;

        return $this;
    }

    public function getIsOn(): ?bool
    {
        return $this->isOn;
    }

    public function setIsOn(bool $isOn): self
    {
        $this->isOn = $isOn;

        return $this;
    }

    public function getApiToken(): ?string
    {
        return $this->apiToken;
    }

    public function setApiToken(?string $apiToken): self
    {
        $this->apiToken = $apiToken;

        return $this;
    }

    public function getInvitePhone(): ?string
    {
        return $this->invitePhone;
    }

    public function setInvitePhone(?string $invitePhone): self
    {
        $this->invitePhone = $invitePhone;

        return $this;
    }

    public function getOpenid(): ?string
    {
        return $this->openid;
    }

    public function setOpenid(?string $openid): self
    {
        $this->openid = $openid;

        return $this;
    }

    public function getPic(): ?string
    {
        return $this->pic;
    }

    public function setPic(?string $pic): self
    {
        $this->pic = $pic;

        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * @return Collection|User[]
     */
    public function getSons(): Collection
    {
        return $this->sons;
    }

    public function addSon(User $son): self
    {
        if (!$this->sons->contains($son)) {
            $this->sons[] = $son;
            $son->setParent($this);
        }

        return $this;
    }

    public function removeSon(User $son): self
    {
        if ($this->sons->contains($son)) {
            $this->sons->removeElement($son);
            // set the owning side to null (unless already changed)
            if ($son->getParent() === $this) {
                $son->setParent(null);
            }
        }

        return $this;
    }

    public function getLevel(): ?UserLevel
    {
        return $this->level;
    }

    public function setLevel(?UserLevel $level): self
    {
        $this->level = $level;

        return $this;
    }

    public function getGroupp(): ?UserGroup
    {
        return $this->groupp;
    }

    public function setGroupp(?UserGroup $groupp): self
    {
        $this->groupp = $groupp;

        return $this;
    }

    public function getPasswordPay(): ?string
    {
        return $this->passwordPay;
    }

    public function setPasswordPay(?string $passwordPay): self
    {
        $this->passwordPay = $passwordPay;

        return $this;
    }
}
